package com.linjiahao.oss.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.linjiahao.oss.service.FileService;
import com.linjiahao.oss.vo.TbTestStateSecretPO;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String upload(MultipartFile file) {
        //获取阿里云存储相关常量
        String endPoint = "ConstantPropertiesUtil.END_POINT";
        String accessKeyId = "ConstantPropertiesUtil.ACCESS_KEY_ID";
        String accessKeySecret = "ConstantPropertiesUtil.ACCESS_KEY_SECRET";
        String bucketName = "ConstantPropertiesUtil.BUCKET_NAME";

        String uploadUrl = null;
        try {
            //判断oss实例是否存在：如果不存在则创建，如果存在则获取
            OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
            if (!ossClient.doesBucketExist(bucketName)) {
                //创建bucket
                ossClient.createBucket(bucketName);
                //设置oss实例的访问权限：公共读
                ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            }
            //获取上传文件流
            InputStream inputStream = file.getInputStream();
            //获取文件名
            String fileName=file.getOriginalFilename();

            //构建日期路径：avatar/2019/02/26/文件名
            String filePath = new DateTime().toString("yyyy/MM/dd");
            //在文件名称中添加唯一随机的值
            String uuid=UUID.randomUUID().toString().replace("-","");
            fileName=uuid+fileName;
            String datePath=new DateTime().toString("yyyy/MM/dd");
            fileName=datePath+"/"+fileName;
            //文件上传至阿里云
            ossClient.putObject(bucketName, fileName, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            //获取url地址
            uploadUrl = "http://" + bucketName + "." + endPoint + "/" + fileName;

        } catch (IOException e) {
            System.out.println(e.getMessage());
            return null;
        }
        return uploadUrl;
    }

    @Override
    public TbTestStateSecretPO insertPO(TbTestStateSecretPO tbTestStateSecretPO) {
        System.out.println(tbTestStateSecretPO.toString());
        redisTemplate.opsForValue().set(tbTestStateSecretPO.getId(), JSON.toJSONString(tbTestStateSecretPO));
        return tbTestStateSecretPO;
    }

    @Override
    public TbTestStateSecretPO getPO(TbTestStateSecretPO tbTestStateSecretPO) {
        System.out.println(tbTestStateSecretPO.toString());
        return JSON.parseObject(redisTemplate.opsForValue().get(tbTestStateSecretPO.getId()),TbTestStateSecretPO.class);
    }

    @Override
    public TbTestStateSecretPO updatePO(TbTestStateSecretPO tbTestStateSecretPO) {
        System.out.println(tbTestStateSecretPO.toString());
        redisTemplate.opsForValue().set(tbTestStateSecretPO.getId(), JSON.toJSONString(tbTestStateSecretPO));
        return tbTestStateSecretPO;
    }
}
